using BW.WMS.CodeEnum;
using BW.WMS.WCL;
using BW.WMS.WCL.DTO;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc;

namespace BW.WMS.Controllers
{
    /// <summary>
    /// 库存管理控制器
    /// </summary>
    [Route("api/wcl/inventory")]
    public class InventoryController : WMSController
    {
        private readonly IInventoryService _inventoryService;

        public InventoryController(IInventoryService inventoryService)
        {
            _inventoryService = inventoryService;
        }

        /// <summary>
        /// 查询库存列表
        /// </summary>
        /// <param name="input">查询条件</param>
        /// <returns>库存查询结果</returns>
        [HttpPost("list")]
        public async Task<ApiResult<InventoryQueryResultDto>> GetInventoryListAsync([FromBody] InventoryQueryDto input)
        {
            return await _inventoryService.GetInventoryListAsync(input);
        }

        /// <summary>
        /// 获取库存统计信息
        /// </summary>
        /// <param name="input">查询条件</param>
        /// <returns>库存统计信息</returns>
        [HttpPost("statistics")]
        public async Task<ApiResult<InventoryStatisticsDto>> GetInventoryStatisticsAsync([FromBody] InventoryQueryDto input)
        {
            return await _inventoryService.GetInventoryStatisticsAsync(input);
        }

        /// <summary>
        /// 导出库存数据
        /// </summary>
        /// <param name="input">查询条件</param>
        /// <returns>导出文件路径</returns>
        [HttpPost("export")]
        public async Task<ApiResult<string>> ExportInventoryAsync([FromBody] InventoryQueryDto input)
        {
            return await _inventoryService.ExportInventoryAsync(input);
        }

        /// <summary>
        /// 获取货品类型列表
        /// </summary>
        /// <returns>货品类型列表</returns>
        [HttpGet("product-types")]
        public async Task<ApiResult<System.Collections.Generic.List<string>>> GetProductTypesAsync()
        {
            return await _inventoryService.GetProductTypesAsync();
        }

        /// <summary>
        /// 获取单位列表
        /// </summary>
        /// <returns>单位列表</returns>
        [HttpGet("units")]
        public async Task<ApiResult<System.Collections.Generic.List<string>>> GetUnitsAsync()
        {
            return await _inventoryService.GetUnitsAsync();
        }

        /// <summary>
        /// 获取库存流水分布（用于"出入库流水库存分布"功能）
        /// </summary>
        /// <param name="productId">货品ID</param>
        /// <param name="warehouseId">仓库ID</param>
        /// <param name="locationId">库位ID</param>
        /// <returns>库存流水信息</returns>
        [HttpGet("flow-distribution")]
        public async Task<ApiResult<object>> GetInventoryFlowDistributionAsync(
            [FromQuery] Guid productId,
            [FromQuery] Guid warehouseId,
            [FromQuery] Guid locationId)
        {
            // 这里可以实现库存流水分布的具体逻辑
            // 暂时返回模拟数据
            return new ApiResult<object>
            {
                Code = ApiEnum.Success,
                Data = new
                {
                    ProductId = productId,
                    WarehouseId = warehouseId,
                    LocationId = locationId,
                    Message = "库存流水分布功能待实现"
                },
                Message = "查询成功"
            };
        }

        /// <summary>
        /// 创建库存记录
        /// </summary>
        /// <param name="input">创建库存信息</param>
        /// <returns>创建结果</returns>
        [HttpPost("create")]
        public async Task<ApiResult<InventoryDetailDto>> CreateInventoryAsync([FromBody] CreateInventoryDto input)
        {
            return await _inventoryService.CreateInventoryAsync(input);
        }

        /// <summary>
        /// 更新库存记录
        /// </summary>
        /// <param name="input">更新库存信息</param>
        /// <returns>更新结果</returns>
        [HttpPut("update")]
        public async Task<ApiResult<InventoryDetailDto>> UpdateInventoryAsync([FromBody] UpdateInventoryDto input)
        {
            return await _inventoryService.UpdateInventoryAsync(input);
        }

        /// <summary>
        /// 删除库存记录
        /// </summary>
        /// <param name="input">删除库存信息</param>
        /// <returns>删除结果</returns>
        [HttpDelete("delete")]
        public async Task<ApiResult<bool>> DeleteInventoryAsync([FromBody] DeleteInventoryDto input)
        {
            return await _inventoryService.DeleteInventoryAsync(input);
        }

        /// <summary>
        /// 根据ID获取库存详情
        /// </summary>
        /// <param name="id">库存记录ID</param>
        /// <returns>库存详情</returns>
        [HttpGet("detail/{id}")]
        public async Task<ApiResult<InventoryDetailDto>> GetInventoryByIdAsync(Guid id)
        {
            return await _inventoryService.GetInventoryByIdAsync(id);
        }
    }
} 